/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Estacionamento.DAO;
/**
 *
 * @author Italo Valença
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import ProjetoCompreJa.ClassesBasicas.Cliente;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class ClienteDAO extends Connect_BD {

    EntityManagerFactory emf;
    EntityManager em;

    public ClienteDAO() {
        emf = Persistence.createEntityManagerFactory("estacionamento_praticodb");
        em = emf.createEntityManager();
    }

    public void CadastrarCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(cli);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void ExcluirCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            /*em.remove(produto);*/
            em.remove(em.getReference(Cliente.class, cli.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }

    public void AlterarCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(cli);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<Cliente> ListarClientes() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public List<Cliente> ListarClientesPorNome(Cliente cli) throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente where cliente.nome = '" + cli.getNome() + "'");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}
